import { payResponse } from '@/enums'

let ispay = false

/**
 * 微信支付
 */
export function requestPayment(result: any) {
  if (ispay)
    return
  ispay = true
  return new Promise<payResponse>((resolve, reject) => {
    try {
      uni.requestPayment({
        // @ts-expect-error
        appid: result.appid,
        provider: 'wxpay',
        timeStamp: `${result.timestamp}`,
        nonceStr: result.nonce_str,
        package: result.package_str,
        signType: 'MD5',
        paySign: result.sign,
        success() {
          ispay = false
          resolve(payResponse.成功)
        },
        fail(_err) {
          ispay = false
          resolve(payResponse.失败)
          reject(payResponse.失败)
        },
        complete: () => {
          resolve(payResponse.完成)
          ispay = false
        },
      })
    }
    catch (e) {
      ispay = false
    }
  })
}
